<template>
  <ele-modal
    width="460px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    title="新建活动文件夹"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="102px">
      <el-form-item label="文件夹名称:" prop="name">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入文件夹名称"
        />
      </el-form-item>
      <div v-if="directorys.length && directorys[0].type==0">
        <el-form-item label="文件夹类型:" prop="typeId">
          <!--1:视频2：音频3：台本4：图片-->
          <el-col>
            <el-radio-group>
              <!---->
              <el-select class="ele-block v-width" v-model="form.typeId" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-radio-group>
          </el-col>
        </el-form-item>
      </div>
      <div class="check-star" v-else>
      <el-form-item label="文件夹类型:">
        <el-col v-if="!directorys.length">
          <el-input
            :disabled="true"
            placeholder="文件夹"
          />
        </el-col>
        <el-col v-else>
          <el-select class="ele-block v-width" v-model="directorys[0].type" disabled>
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      </div>
      <el-form-item label="公共文件夹:" prop="isCommon">
        <el-radio-group v-model="form.isCommon">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import {addUserFile} from '@/api/creatCenter/material';

  export default {
    props: {
      // 文件夹数据
      directorys: Array,
      // 弹窗是否打开
      visible: Boolean,
      // 父级 id
      parentId: Number,
    },
    data() {
      return {
        options: [
          /*{
            value: 0,
            label: '文件夹'
          },*/
          {
            value: 1,
            label: '视频'
          }, {
            value: 2,
            label: '音频'
          }, {
            value: 3,
            label: '台本'
          }, {
            value: 4,
            label: '图片'
          }],
        // 表单数据
        form: {
          name: '',
          typeId:'',
          isCommon: 0,
        },
        // 表单验证规则
        rules: {
          name: [
            {
              required: true,
              message: '请输入文件夹名称',
              trigger: 'blur'
            }
          ],
          typeId: [
            {
              required: true,
              message: '请选择文件夹类型',
              trigger: 'blur'
            }
          ], isCommon: [
            {
              required: true,
              message: '请选择是否公共文件夹',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false
      };
    },
    methods: {
      //类型选择
      typeClick(e) {
        console.log('e=', e, 'typeId', this.typeId, 'ispublic', this.ispublic)
      },
      show(){
        //
        console.log("进入show======",this.directorys.length)
        if(this.directorys.length==0){
          this.form.typeId=0
        }
        else {
          if(this.directorys[0].type==0){
            this.form.typeId=''
          }
          else{
            this.form.typeId=this.directorys[0].type
          }
        }
      },
      /* 保存编辑 */
      save() {
        console.log('this.directorys.length',this.directorys.length)
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;

          addUserFile({
            // ...this.form,
            // parentId: this.parentId,
            // isDirectory: 1
            username: this.$store.state.user.info.username,
            parentId: this.parentId,
            type: this.form.typeId,
            name: this.form.name,
            isCommon: this.form.isCommon,
          })
            .then((msg) => {
              this.loading = false;
              this.form.isCommon=0;
              this.form.typeId='';
              console.log("成功", msg)
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              console.log("失败e", e)
              this.loading = false;
              this.$message.error(e);
            });
        });
      },
      /* 更新 visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (!visible) {
          this.$refs.form.clearValidate();
          this.form.name = '';
        }
      }
    }
  };
</script>
<style>
  .v-width{
    width: 318px;
  }
</style>
